package com.lancoo.klgcourseware.ui.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import androidx.annotation.NonNull;
import android.text.style.ImageSpan;
import android.util.Log;
import android.widget.TextView;

import com.lancoo.klgcourseware.R;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by pengmi on 2019/11/11 21:05
 * Description:
 */
public class VolumeAlignImageSpan extends ImageSpan {

    private Context mContext;
    private final int[] volumeResArray= {R.drawable.klg_volume_cyan_mini_3,R.drawable.klg_volume_cyan_mini_1,R.drawable.klg_volume_cyan_mini_2};
    private int showVolumeIndex;
    private Timer timer;
    private TimerTask timerTask;

    public VolumeAlignImageSpan(Context mContext, Bitmap b) {
        super(mContext,b);
        this.mContext=mContext.getApplicationContext();
    }

    @Override
    public void draw(@NonNull Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom,
                     @NonNull Paint paint) {
        if(mContext==null){
            return;
        }
        Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), volumeResArray[showVolumeIndex]);
        BitmapDrawable bd =new BitmapDrawable(bitmap);
        int width = bd.getIntrinsicWidth();
        int height = bd.getIntrinsicHeight();
        bd.setBounds(0, 0, Math.max(width, 0), Math.max(height, 0));
//        Paint.FontMetricsInt fm = paint.getFontMetricsInt();
        canvas.save();
        Paint.FontMetricsInt fmPaint = paint.getFontMetricsInt();
        int fontHeight = fmPaint.descent - fmPaint.ascent;
        int centerY = y + fmPaint.descent - fontHeight / 2;
        int transY = centerY - (bd.getBounds().bottom - bd.getBounds().top) / 2;
        canvas.translate(x, transY);//绘制图片位移一段距离
        bd.draw(canvas);
        canvas.restore();
    }

    public void startVolumeAnimation(final TextView textView){
        timer=new Timer();
        timerTask=new TimerTask() {
            @Override
            public void run() {
                Log.e("aaaa","showVolumeIndex:"+showVolumeIndex);
                if(showVolumeIndex==2){
                    showVolumeIndex=0;
                }else{
                    showVolumeIndex++;
                }
                textView.post(new Runnable() {
                    @Override
                    public void run() {
                        if(textView!=null)
                            textView.invalidate();
                    }
                });
            }
        };
        mContext=textView.getContext();
        timer.schedule(timerTask,500,500);
    }

    public void stopVolumeAnimation(TextView textView){
        if(timer!=null){
            timerTask.cancel();
            timer.cancel();

        }
        showVolumeIndex=0;
        textView.invalidate();

    }

    public void destoryVolumeImageSpan(TextView textView){
        showVolumeIndex=0;
        textView.invalidate();
        if(timer !=null){
            timer.cancel();
            timer =null;
        }
        if(timerTask !=null){
            timerTask.cancel();
            timerTask =null;
        }
    }


}
